package com.ruoyi.web.controller.apply.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.activity.service.IActivityService;
import com.ruoyi.activity.service.IApplyService;
import com.ruoyi.activity.service.IContestService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.model.activity.Activity;
import com.ruoyi.model.apply.Apply;
import com.ruoyi.model.apply.excel.ExcelApplyVO;
import com.ruoyi.model.template.TemplateValue;
import com.ruoyi.ucenter.service.IUserInfoService;
import com.ruoyi.web.utils.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报名信息Controller
 *
 * @author 张怀秋
 * @date 2024-04-27
 */
@Api(tags = "后台:报名信息")
@RestController
@RequestMapping("/admin/apply/info")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ApplyAdminController extends BaseController {

    /**
     * 报名服务
     */
    private final IApplyService applyService;


    /**
     * 查询报名信息列表
     */
    @PreAuthorize("@ss.hasPermi('apply:info:list')")
    @GetMapping("/list")
    @ApiOperation("列表")
    public TableDataInfo list(Apply apply) {
        startPage();
        List<Apply> list = applyService.selectApplyList(apply);
        return getDataTable(list);
    }

    /**
     * 导出报名信息列表
     */
    @PreAuthorize("@ss.hasPermi('apply:info:export')")
    @Log(title = "报名信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, String id) {
//        List<Apply> list = applyService.selectApplyList(apply);
//        ExcelUtil<Apply> util = new ExcelUtil<>(Apply.class);
//        util.exportExcel(response, list, "报名信息数据");
        //导出报名信息
        Apply item = applyService.selectApplyById(id);
        try {
            WebUtils.setDownLoadHeader("用户报名信息.xlsx", response);
            String content = item.getContent();
            List<TemplateValue> list = JSON.parseObject(content,
                    new TypeReference<List<TemplateValue>>() {
            });
            List<ExcelApplyVO> result = list.stream().map(value -> {
                ExcelApplyVO excelApplyVO = new ExcelApplyVO();
                excelApplyVO.setFrame(value.getName());
                excelApplyVO.setCorrespondingValue(value.getValue());
                return excelApplyVO;
            }).collect(Collectors.toList());
            EasyExcelFactory.write(response.getOutputStream(), ExcelApplyVO.class)
                    .autoCloseStream(Boolean.FALSE).sheet("模板")
                    .doWrite(result);
        } catch (Exception e) {
            // 出现异常响应Json
            R<Object> result = R.fail(500, "导出失败!");
            WebUtils.renderString(response, JSON.toJSONString(result));
        }
    }

    /**
     * 获取报名信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('apply:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(applyService.selectApplyById(id));
    }

//    /**
//     * 新增报名信息
//     */
//    @PreAuthorize("@ss.hasPermi('apply:info:add')")
//    @Log(title = "报名信息", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody Apply apply) {
//        return toAjax(applyService.insertApply(apply));
//    }

    /**
     * 修改报名信息
     */
    @PreAuthorize("@ss.hasPermi('apply:info:edit')")
    @Log(title = "报名信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Apply apply) {
        return toAjax(applyService.updateApply(apply));
    }

    /**
     * 删除报名信息
     */
    @PreAuthorize("@ss.hasPermi('apply:info:remove')")
    @Log(title = "报名信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(applyService.deleteApplyByIds(ids));
    }

}
